package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.minio.MinioProperties;
import com.atguigu.lease.web.admin.service.FileService;
import io.minio.*;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinioProperties minioProperties;

    @Override
    public String upload(MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {

        //1. 判断有没有桶
        boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperties.getBucketName()).build());
        //2. 没有桶创建桶
        if (!found){
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProperties.getBucketName()).build());
        }
        //3. 设置桶的访问权限
        String policy = """
                        {
                          "Statement" : [ {
                            "Action" : "s3:GetObject",
                            "Effect" : "Allow",
                            "Principal" : "*",
                            "Resource" : "arn:aws:s3:::%s/*"
                          } ],
                          "Version" : "2012-10-17"
                        }""".formatted(minioProperties.getBucketName()); // [ String ]  %s -> ?
        minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(minioProperties.getBucketName()).config(policy).build());

        //4. 对象名的重复处理
        String filename = file.getOriginalFilename();  //bet.png 重复 |  按日期分类 | 按月分类

        String objectName = new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/" + UUID.randomUUID() + "-"+ filename ;

        //5. 上传文件
        //上传图片
//        minioClient.uploadObject(
//                UploadObjectArgs.builder()
//                        .bucket(minioProperties.getBucketName())
//                        .object(objectName)
//                        //本地文件上传 本地地址
//                        //.filename("D:\\gyy.png")
//                        .build());

        minioClient.putObject(PutObjectArgs.builder().
                bucket(minioProperties.getBucketName()).
                object(objectName).
                stream(file.getInputStream(), file.getSize(), -1).
                contentType(file.getContentType()).build());

        //返回一个url地址
        //String url = minioProperties.getEndpoint() + "/" + minioProperties.getBucketName() + "/"+ objectName;
        String.join("/",minioProperties.getEndpoint(),minioProperties.getBucketName(),objectName);
        return String.join("/",minioProperties.getEndpoint(),minioProperties.getBucketName(),objectName);
    }
}
